package edu.scnu.fans.fingertip.user.controller;

import edu.scnu.fans.fingertip.common.api.ApiResponse;
import edu.scnu.fans.fingertip.common.api.IdRequest;
import edu.scnu.fans.fingertip.common.api.ResponseUtils;
import edu.scnu.fans.fingertip.common.api.RowResponse;
import edu.scnu.fans.fingertip.common.api.HeaderConstant;
import edu.scnu.fans.fingertip.user.api.UserAddressApi;
import edu.scnu.fans.fingertip.user.input.UserAddressDto;
import edu.scnu.fans.fingertip.user.service.UserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "收货地址API")
public class UserAddressController implements UserAddressApi {

    @Resource
    private UserAddressService userAddressService;


    @PostMapping("/fingertip/user/address/v1/list")
    @ApiOperation("收货地址列表")
    public RowResponse<List<UserAddressDto>> list(@RequestHeader(HeaderConstant.USER_ID) Long userId) {
        return ResponseUtils.rows(userAddressService.list(userId));
    }

    @PostMapping("/fingertip/user/address/v1/findAddressById")
    @ApiOperation("根据id查找收货地址")
    @Override
    public ApiResponse<UserAddressDto> findAddressById(@RequestHeader(HeaderConstant.USER_ID) Long userId,
                                                       @RequestBody @Valid IdRequest input) {
        UserAddressDto output = userAddressService.findAddressById(userId, input);
        return new ApiResponse<>(output);
    }

    @PostMapping("/fingertip/user/address/v1/add")
    @ApiOperation("添加收货地址")
    public ApiResponse<?> add(@RequestHeader(HeaderConstant.USER_ID) Long userId,
                              @RequestBody @Valid UserAddressDto input) {
        userAddressService.add(userId, input);
        return ResponseUtils.EMPTY;
    }

    @PostMapping("/fingertip/user/address/v1/update")
    @ApiOperation("更新收货地址")
    public ApiResponse<?> update(@RequestHeader(HeaderConstant.USER_ID) Long userId,
                                 @RequestBody @Valid UserAddressDto input) {
        userAddressService.update(userId, input);
        return ResponseUtils.EMPTY;
    }

    @PostMapping("/fingertip/user/address/v1/delete")
    @ApiOperation("删除收货地址")
    public ApiResponse<?> delete(@RequestHeader(HeaderConstant.USER_ID) Long userId,
                                 @RequestBody @Valid IdRequest input) {
        userAddressService.delete(userId, input);
        return ResponseUtils.EMPTY;
    }

    @PostMapping("/fingertip/user/address/v1/setDefaultAddress")
    @ApiOperation("设置默认收货地址")
    public ApiResponse<?> setDefaultAddress(@RequestHeader(HeaderConstant.USER_ID) Long userId,
                                            @RequestBody @Valid IdRequest input) {
        userAddressService.setDefaultAddress(userId, input);
        return ResponseUtils.EMPTY;
    }

}
